package com.bac.rentmap.web.ad.user.review;

import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import javax.servlet.http.HttpSession;

public class UserReviewHelper {
    private static final Logger log = Logger.getLogger(UserReviewHelper.class
            .getName());

    public static final String USER_REVIEWS = "userReviews";

    @SuppressWarnings("unchecked")
    public static boolean hasUserReview(HttpSession session, Long flatAdId){
        Set<Long> userReviews = (Set<Long>) session.getAttribute(USER_REVIEWS);
        return hasUserReview(userReviews, flatAdId);
    }

    public static boolean hasUserReview(Set<Long> userReviews, Long flatAdId){
        if(userReviews == null){
            return false;
        }
        return userReviews.contains(flatAdId);
    }

    @SuppressWarnings("unchecked")
    public static void registerUserReview(HttpSession session, Long flatAdId){
        log.fine("Registering user review for " + flatAdId);
        Set<Long> userReviews = (Set<Long>) session.getAttribute(USER_REVIEWS);
        if(userReviews == null){
            userReviews = new HashSet<Long>();
            session.setAttribute(USER_REVIEWS, userReviews);
        }
        userReviews.add(flatAdId);
    }
}
